package com.thought.wxsession.consumer;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.exceptions.ExceptionUtil;
import com.alibaba.fastjson.JSONObject;
import com.thought.wxsession.chain.EmotionChain;
import com.thought.wxsession.chain.ImageChain;
import com.thought.wxsession.chain.TextChain;
import com.thought.wxsession.chain.WxMsgChain;
import com.thought.wxsession.cons.MsgType;
import com.thought.wxsession.entity.WxMsg;
import com.thought.wxsession.service.IWxMsgService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.chain.Context;
import org.apache.commons.chain.impl.ChainBase;
import org.apache.commons.chain.impl.ContextBase;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

import static com.thought.wxsession.cons.ChainCons.MSG_ID;
import static com.thought.wxsession.cons.ChainCons.MSG_TYPE;
import static com.thought.wxsession.cons.MqCons.*;
import static com.thought.wxsession.cons.ParsingStatus.PARSING_FAILED;

/**
 * @Author：zhuqinyue
 * @Package：com.wecom.consumer
 * @Project：customer-service-application
 * @name：QrCodeConsumer
 * @Date：2022/11/29 12:22
 * @Filename：QrCodeConsumer
 */
@Component
@Slf4j
public class FlieMessageConsumer{

    @Autowired
    private WxMsgChain wxMsgChain;

    @Autowired
    private IWxMsgService wxMsgService;


    @RabbitListener(bindings =
            {
                    @QueueBinding(
                            value = @Queue(WX_MEAAGES_HANDEL_QUEUE),
                            exchange = @Exchange(value = WX_MEAAGES_HANDEL_EXCHANGE, delayed = Exchange.TRUE),
                            key = WX_MEAAGES_HANDEL_KEY)
            })
    public void consume(List<WxMsg> messages) {
        log.info("开始消费解析{}", JSONObject.toJSONString(messages));
        try {
            if (CollectionUtil.isEmpty(messages)) {
                log.info("消费结束！消息list为空");
                return;
            }
            List<WxMsg> wxMsgs = new ArrayList<>();
            messages.forEach(msg -> {
                if (msg.getErrorCode() == 0) {
                    Context context = new ContextBase();
                    context.put(MSG_TYPE,msg.getMsgtype());
                    context.put(MSG_ID,msg.getId());
                    try {
                        wxMsgChain.execute(context);
                    } catch (Exception e) {
                        log.error("解析消息出错",e);
                        msg.setParsingStatus(    PARSING_FAILED.getCode());
                        msg.setParsingDesc(ExceptionUtil.stacktraceToString(e));
                        wxMsgs.add(msg);
                    }
                }
            });
            if (CollectionUtil.isNotEmpty(wxMsgs)) {
                wxMsgService.updateBatchById(wxMsgs);
            }
            log.info("消息解析结束");
        } catch (Exception e) {
            log.error("解析出现异常",e);
        }


    }
}
